package com.sailfish.springbootdemo.dao.db2;

import com.sailfish.springbootdemo.common.DB2TableName;
import com.sailfish.springbootdemo.pojo.db2.PcbCapacityData;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.type.JdbcType;

import java.util.Date;
import java.util.List;

public interface PcbCapacityDataDao {

    @Results(id = "pcbCapacityDataResultMap", value = {
            @Result(column = "pcb_capacity_data_id", property = "pcbCapacityDataId", jdbcType = JdbcType.INTEGER),
            @Result(column = "pcb_capacity_history_id", property = "pcbCapacityHistoryId", jdbcType = JdbcType.INTEGER),
            @Result(column = "pcb_factory_id", property = "pcbFactoryId", jdbcType = JdbcType.INTEGER),
            @Result(column = "pcb_capacity_param_id", property = "pcbCapacityParamId", jdbcType = JdbcType.INTEGER),
            @Result(column = "pcb_param_value_type", property = "pcbParamValueType", jdbcType = JdbcType.INTEGER),
            @Result(column = "pcb_param_key", property = "pcbParamKey", jdbcType = JdbcType.VARCHAR),
            @Result(column = "pcb_param_value", property = "pcbParamValue", jdbcType = JdbcType.VARCHAR),
            @Result(column = "create_time", property = "createTime", jdbcType = JdbcType.DATETIMEOFFSET),
            @Result(column = "update_time", property = "updateTime", jdbcType = JdbcType.DATETIMEOFFSET),
            @Result(column = "status", property = "status", jdbcType = JdbcType.INTEGER),
            @Result(column = "sort", property = "sort", jdbcType = JdbcType.INTEGER),
            @Result(column = "create_user", property = "createUser", jdbcType = JdbcType.VARCHAR),
            @Result(column = "update_user", property = "updateUser", jdbcType = JdbcType.VARCHAR),
    })

    /*
     * 查询单个
     */
    @Select("<script> " +
            "select *" +
            " from " + DB2TableName.PCB_CAPAICTY_DATA +
            "<where> " +
            "<if test='id != null'>" +
            " and pcb_capacity_data_id = #{id}" +
            "</if>" +
            "</where> " +
            " order by sort" +
            "</script>")
    List<PcbCapacityData> getById(@Param("id") Integer id);

    /*
     *查询指定行数据
     */
    @Select("<script> " +
            "select pcb_capacity_data_id, pcb_capacity_history_id, pcb_factory_id, pcb_capacity_param_id, pcb_param_value_type, pcb_param_key, pcb_param_value, create_time, create_user, update_time, update_user, status, sort " +
            "from " + DB2TableName.PCB_CAPAICTY_DATA +
            "<where>" +
            "<if test='id != null'>" +
            "    and pcb_capacity_data_id = #{id}" +
            "</if>" +
            "<if test='pcbCapacityHistoryId != null '>" +
            "    and pcb_capacity_history_id = #{pcbCapacityHistoryId}" +
            "</if>" +
            "<if test='pcbFactoryId != null '>" +
            "    and pcb_factory_id = #{pcbFactoryId}" +
            "</if>" +
            "<if test='pcbFactoryId == null '>" +
            "    and pcb_factory_id is null" +
            "</if>" +
            "<if test='pcbCapacityParamId != null '>" +
            "    and pcb_capacity_param_id = #{pcbCapacityParamId}" +
            "</if>" +
            "<if test='pcbParamValueType != null '>" +
            "    and pcb_param_value_type = #{pcbParamValueType}" +
            "</if>" +
            "<if test='pcbParamKey != null and pcbParamKey != \"\" '>" +
            "    and pcb_param_key = #{pcbParamKey}" +
            "</if>" +
            "<if test='pcbParamValue != null and pcbParamValue != \"\" '>" +
            "    and pcb_param_value = #{pcbParamValue}" +
            "</if>" +
            "<if test='sort != null'>" +
            "    and sort = #{sort}" +
            "</if>" +
            "<if test='status != null'>" +
            "    and status = #{status}" +
            "</if>" +
            "<if test='createTime != null'>" +
            "    and create_time = #{createTime}" +
            "</if>" +
            "<if test='updateTime != null'>" +
            "    and update_time = #{updateTime}" +
            "</if>" +
            "<if test='createUser != null and createUser != \"\"'>" +
            "    and create_user = #{createUser}" +
            "</if>" +
            "<if test='updateUser != null and updateUser != \"\"'>" +
            "    and update_user = #{updateUser}" +
            "</if>" +
            "</where>" +
            " order by sort" +
//            "limit #{pageable.offset}, #{pageable.pageSize}"+
            "</script>")
    @ResultMap(value = "pcbCapacityDataResultMap")
    List<PcbCapacityData> getAllByLimit(Integer id, Integer pcbCapacityHistoryId, Integer pcbFactoryId, Integer pcbCapacityParamId, Integer pcbParamValueType,
                                        String pcbParamKey, String pcbParamValue, Date createTime, String createUser, Date updateTime, String updateUser,
                                        Integer status, Integer sort);

    /*
     *统计总行数
     */
    @Select("<script> " +
            "select count(1) " +
            "from " + DB2TableName.PCB_CAPAICTY_DATA +
            "</script>")
    Integer getAllCount();

    /*
     *新增所有列
     */
    @Insert("insert into " + DB2TableName.PCB_CAPAICTY_DATA + " (pcb_capacity_history_id, pcb_factory_id, pcb_capacity_param_id, pcb_param_value_type, pcb_param_key, pcb_param_value, create_time, create_user, update_time, update_user, status, sort) values " +
            "(#{pcbCapacityHistoryId}, #{pcbFactoryId}, #{pcbCapacityParamId}, #{pcbParamValueType}, #{pcbParamKey}, #{pcbParamValue}, #{createTime}, #{createUser}, #{updateTime}, #{updateUser}, #{status}, #{sort})")
    @Options(useGeneratedKeys = true, keyProperty = "pcbCapacityDataId")
    Integer insert(PcbCapacityData param);

    /*
     *批量新增
     */
    @Insert("insert into " + DB2TableName.PCB_CAPAICTY_DATA + " (pcb_capacity_history_id, pcb_factory_id, pcb_capacity_param_id, pcb_param_value_type, pcb_param_key, pcb_param_value, create_time, create_user, update_time, update_user, status, sort) values " +
            "<foreach collection=\"entities\" item=\"entity\" separator=\",\">" +
            "(#{entity.pcbCapacityHistoryId}, #{entity.pcbFactoryId}, #{entity.pcbCapacityParamId}, #{entity.pcbParamValueType}, #{entity.pcbParamKey}, #{entity.pcbParamValue}, #{entity.createTime}, #{entity.createUser}, #{entity.updateTime}, #{entity.updateUser}, #{entity.status}, #{entity.sort})")
    Integer insertBatch(@Param("entities") List<PcbCapacityData> entities);

    /*
     *通过主键删除
     */
    @Delete("delete from " + DB2TableName.PCB_CAPAICTY_DATA + " where pcb_capacity_data_id = #{id} ")
    Integer delete(@Param("id") Integer id);

    @Delete("<script>" +
            "delete from " + DB2TableName.PCB_CAPAICTY_DATA +
            "<where>" +
            "<if test='factoryId != null '>" +
            " and pcb_factory_id = #{factoryId}" +
            "</if>" +
            "<if test='factoryId == null '>" +
            " and pcb_factory_id is null" +
            "</if>" +
            "<if test='valueType != null '>" +
            " and pcb_param_value_type = #{valueType}" +
            "</if>" +
            "</where>" +
            "</script>")
    Integer deleteByValueType(@Param("valueType") Integer valueType, @Param("factoryId") Integer factoryId);

    @Delete("delete from " + DB2TableName.PCB_CAPAICTY_DATA + " where  pcb_capacity_data_id in " +
            "<foreach collection=\"ids\" item = \"id\" open=\"(\" separator=\",\" close=\")\">  " +
            "#{id} " +
            "</foreach>"
    )
    Integer deleteBatch(@Param("ids") List<Integer> ids);

    @Delete("delete from " + DB2TableName.PCB_CAPAICTY_DATA + " where pcb_capacity_param_id = #{paramId} ")
    Integer deleteByParamId(@Param("paramId") Integer paramId);


    @Delete("delete from " + DB2TableName.PCB_CAPAICTY_DATA + " where pcb_factory_id = #{factoryId}  ")
    Integer deleteByFactoryId(Integer factoryId, Integer historyId);

    /*
     *通过主键修改数据
     */
    @Update("<script>" +
            "update " + DB2TableName.PCB_CAPAICTY_DATA +
            "<set>" +
            "<if test='pcbCapacityHistoryId != null '>" +
            "    pcb_capacity_history_id = #{pcbCapacityHistoryId}," +
            "</if>" +
            "<if test='pcbFactoryId != null '>" +
            "    pcb_factory_id = #{pcbFactoryId}," +
            "</if>" +
            "<if test='pcbCapacityParamId != null '>" +
            "    pcb_capacity_param_id = #{pcbCapacityParamId}," +
            "</if>" +
            "<if test='pcbParamValueType != null '>" +
            "    pcb_param_value_type = #{pcbParamValueType}," +
            "</if>" +
            "<if test='pcbParamKey != null and pcbParamKey != \"\" '>" +
            "    pcb_param_key = #{pcbParamKey}," +
            "</if>" +
            "<if test='pcbParamValue != null and pcbParamValue != \"\" '>" +
            "    pcb_param_value = #{pcbParamValue}," +
            "</if>" +
            "<if test='sort != null'>" +
            "    sort = #{sort}," +
            "</if>" +
            "<if test='status != null'>" +
            "    status = #{status}," +
            "</if>" +
            "<if test='createTime != null'>" +
            "    create_time = #{createTime}," +
            "</if>" +
            "<if test='updateTime != null'>" +
            "    update_time = #{updateTime}," +
            "</if>" +
            "<if test='createUser != null and createUser != \"\"'>" +
            "    create_user = #{createUser}," +
            "</if>" +
            "<if test='updateUser != null and updateUser != \"\"'>" +
            "    update_user = #{updateUser}," +
            "</if>" +
            "</set>" +
            "where pcb_capacity_data_id = #{pcbCapacityDataId}" +
            "</script>")
    Integer update(PcbCapacityData param);

    /*
     *批量修改数据
     */
    @Update("<script>" +
            "update " + DB2TableName.PCB_CAPAICTY_DATA +
            "<foreach collection=\"entities\" item=\"entity\" separator=\",\">" +
            "<set>" +
            "<if test='#{entity.pcbCapacityHistoryId} != null '>" +
            "    pcb_capacity_history_id = #{entity.pcbCapacityHistoryId}," +
            "</if>" +
            "<if test='#{entity.pcbFactoryId} != null '>" +
            "    pcb_factory_id = #{entity.pcbFactoryId}," +
            "</if>" +
            "<if test='#{entity.pcbCapacityParamId} != null '>" +
            "    pcb_capacity_param_id = #{entity.pcbCapacityParamId}," +
            "</if>" +
            "<if test='#{entity.pcbParamValueType} != null '>" +
            "    pcb_param_value_type = #{entity.pcbParamValueType}," +
            "</if>" +
            "<if test='#{entity.pcbParamKey} != null and #{entity.pcbParamKey} != \"\" '>" +
            "    pcb_param_key = #{entity.pcbParamKey}," +
            "</if>" +
            "<if test='#{entity.pcbParamValue} != null and #{entity.pcbParamValue} != \"\" '>" +
            "    pcb_param_value = #{entity.pcbParamValue}," +
            "</if>" +
            "<if test='#{entity.sort} != null'>" +
            "    sort = #{entity.sort}," +
            "</if>" +
            "<if test='#{entity.status} != null'>" +
            "    status = #{entity.status}," +
            "</if>" +
            "<if test='#{entity.createTime} != null'>" +
            "    create_time = #{entity.createTime}," +
            "</if>" +
            "<if test='#{entity.updateTime} != null'>" +
            "    update_time = #{entity.updateTime}," +
            "</if>" +
            "<if test='#{entity.createUser} != null and #{entity.createUser} != \"\"'>" +
            "    create_user = #{entity.createUser}," +
            "</if>" +
            "<if test='#{entity.updateUser} != null and #{entity.updateUser} != \"\"'>" +
            "    update_user = #{entity.updateUser}," +
            "</if>" +
            "</set>" +
            "</foreach>" +
            "where pcb_capacity_history_id = #{entity.pcbCapacityHistoryId} and pcb_factory_id = #{entity.pcbFactoryId} and pcb_param_key = #{entity.pcbParamKey}" +
            "</script>")
    Integer updateBatch(@Param("entities") List<PcbCapacityData> entities);

    @Insert("<script>" +
            "insert into tb_pcb_capacity_data(pcb_capacity_history_id, pcb_factory_id, pcb_capacity_param_id, pcb_param_value_type, pcb_param_key, pcb_param_value, create_time, create_user, update_time, update_user, status, sort) " +
            "values" +
            "<foreach collection=\"entities\" item=\"entity\" separator=\",\">" +
            "(#{entity.pcbCapacityHistoryId}, #{entity.pcbFactoryId}, #{entity.pcbCapacityParamId}, #{entity.pcbParamValueType}, #{entity.pcbParamKey}, #{entity.pcbParamValue}, #{entity.createTime}, #{entity.createUser}, #{entity.updateTime}, #{entity.updateUser}, #{entity.status}, #{entity.sort}) " +
            "</foreach>" +
            "on duplicate key update " +
            "pcb_capacity_history_id = values(pcb_capacity_history_id)," +
            "pcb_factory_id = values(pcb_factory_id)," +
            "pcb_capacity_param_id = values(pcb_capacity_param_id)," +
            "pcb_param_value_type = values(pcb_param_value_type)," +
            "pcb_param_key = values(pcb_param_key)," +
            "pcb_param_value = values(pcb_param_value)," +
            "create_time = values(create_time)," +
            "create_user = values(create_user)," +
            "update_time = values(update_time)," +
            "update_user = values(update_user)," +
            "status = values(status)," +
            "sort = values(sort)" +
            "</script>")
    @Options(useGeneratedKeys = true, keyProperty = "pcbCapacityDataId")
    Integer insertOrUpdateBatch(@Param("entities") List<PcbCapacityData> entities);

    @Delete("delete from " + DB2TableName.PCB_CAPAICTY_DATA)
    Integer deleteAll();
}
